﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using NHibernate.Criterion;
using OurSite.Business.Picture;

namespace OurSite.Business.Repositories
{
    public class PeopleRepository : BaseRepository, IPeopleRepository
    {
        public void Add(People people)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.SaveOrUpdate(people);
                transaction.Commit();
            }
        }

        public void DeletePeopleFromPicture(Guid pictureId)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                Object result = session.CreateSQLQuery("DELETE FROM PicturePeople WHERE Picture_ID = :pictureId")
                    .SetParameter("pictureId", pictureId)
                    .UniqueResult();

                transaction.Commit();
            }
        }

        public void AddPeopleToPicture(Guid pictureId, Guid peopleId)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                Object result = session.CreateSQLQuery("INSERT INTO PicturePeople (Picture_ID, People_ID) VALUES (:pictureId, :peopleId)")
                    .SetParameter("pictureId", pictureId)
                    .SetParameter("peopleId", peopleId)
                    .UniqueResult();

                transaction.Commit();
            }
        }

        public IList<People> GetAllPeople()
        {
            using (var tx = session.BeginTransaction())
            {
                var people = session.CreateCriteria<People>()
                    .AddOrder(Order.Desc("IsHuman"))
                    .AddOrder(Order.Asc("LastName"))
                    .AddOrder(Order.Asc("FirstName"))
                    .List<People>();

                tx.Commit();
                return people;
            }
        }

        public IList<People> GetAllHumanPeople()
        {
            using (var tx = session.BeginTransaction())
            {
                var people = session.CreateCriteria<People>()
                    .Add(Restrictions.Eq(Projections.Property("IsHuman"), true))
                    .List<People>();

                tx.Commit();
                return people;
            }
        }

        public People GetPeopleByName(string name)
        {
            using (var tx = session.BeginTransaction())
            {
                var people = session.CreateCriteria<People>()
                    .List<People>();
                People person = people.Single(p => p.Name == name);

                tx.Commit();
                return person;
            }
        }

        public People GetPeopleById(Guid peopleId)
        {
            try
            {
                using (var tx = session.BeginTransaction())
                {
                    var people = session.CreateCriteria<People>().List<People>();

                    People person = people.Single(p => p.PeopleId == peopleId);

                    tx.Commit();
                    return person;
                }
            }catch(Exception ex){}

            return null;
        }

        public List<PeopleLight> GetAllPeopleLight()
        {
            IList<People> ppl = GetAllPeople();

            var ppll = (from per in ppl select new PeopleLight(per)).ToList();

            return ppll;
        }
    }
}
